package com.cmfi.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.EquipmentDao;
import com.cmfi.domain.Equipment;
import com.cmfi.service.inter.EquipmentService;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentDao equipmentDao;
    
    Logger log = LoggerFactory.getLogger(getClass());

    public boolean save(Equipment equipment) {
        return equipmentDao.save(equipment) > 0;
    }
    
    public boolean update(Equipment equipment, Long id) {
    	equipment.setId(id);
        return equipmentDao.update(equipment) > 0;
    }
    
    public boolean delete(Long id) {
        return equipmentDao.delete(id) > 0;
    }
    
    @Override
	public List<Equipment> findByPage(Long pageNum, Long pageSize) throws Exception {
		log.info("[EquipmentServiceImpl][findByPage] pageNum:{},pageSize:{}", pageNum, pageSize);
		Long allNum = equipmentDao.count();
		long start = (pageNum - 1) * pageSize;
		if (start > allNum) {
			throw new Exception("请求的页已经不存在");
		}
		List<Equipment> equipmentList = equipmentDao.findByPage(start, pageSize);
		return equipmentList;
	}
    
    @Override
	public List<Equipment> findByPageAndStatus(Long pageNum, Long pageSize) throws Exception {
		log.info("[EquipmentServiceImpl][findByPage] pageNum:{},pageSize:{}", pageNum, pageSize);
		Long allNum = equipmentDao.count();
		long start = (pageNum - 1) * pageSize;
		if (start > allNum) {
			throw new Exception("请求的页已经不存在");
		}
		List<Equipment> equipmentList = equipmentDao.findByPageAndStatus(start, pageSize);
		return equipmentList;
	}
    
    @Override
	public List<Equipment> findByPageAndWarnings(Long pageNum, Long pageSize) throws Exception {
		log.info("[EquipmentServiceImpl][findByPage] pageNum:{},pageSize:{}", pageNum, pageSize);
		Long allNum = equipmentDao.count();
		long start = (pageNum - 1) * pageSize;
		if (start > allNum) {
			throw new Exception("请求的页已经不存在");
		}
		List<Equipment> equipmentList = equipmentDao.findByPageAndWarnings(start, pageSize);
		return equipmentList;
	}
}
